package com.yuanheng100.ems4j.mapper;

import com.yuanheng100.ems4j.model.Ems4jApplication;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.caches.ehcache.EhcacheCache;

import java.util.List;

@CacheNamespace(implementation = EhcacheCache.class)
public interface Ems4jApplicationMapper
{

    /**
     * 查询所有的应用
     * @return
     */
    @Select({
            "SELECT",
            "appid, appname, appsource, appstatus",
            "FROM ems4j_application"
    })
    @Results({
            @Result(column = "appid", property = "appid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "appname", property = "appname", jdbcType = JdbcType.VARCHAR),
            @Result(column = "appsource", property = "appsource", jdbcType = JdbcType.VARCHAR),
            @Result(column = "appstatus", property = "appstatus", jdbcType = JdbcType.SMALLINT)
    })
    List<Ems4jApplication> selectAll();

    /**
     * 根据应用ID查询唯一应用
     * @param appid
     * @return
     */
    @Select({
            "SELECT",
            "appid, appname, appsource, appstatus",
            "FROM ems4j_application",
            "WHERE appid = #{appid,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "appid", property = "appid", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "appname", property = "appname", jdbcType = JdbcType.VARCHAR),
            @Result(column = "appsource", property = "appsource", jdbcType = JdbcType.VARCHAR),
            @Result(column = "appstatus", property = "appstatus", jdbcType = JdbcType.SMALLINT)
    })
    Ems4jApplication selectByPrimaryKey(Integer appid);

    /**
     * 插入一条新的应用记录
     * @param record
     * @return
     */
    @Insert({
            "INSERT INTO ems4j_application (appid, appname, ",
            "appsource, appstatus)",
            "VALUES (#{appid,jdbcType=INTEGER}, #{appname,jdbcType=VARCHAR}, ",
            "#{appsource,jdbcType=VARCHAR}, #{appstatus,jdbcType=SMALLINT})"
    })
    int insert(Ems4jApplication record);

    @Update({
            "UPDATE ems4j_application",
            "SET appname = #{appname,jdbcType=VARCHAR},",
            "appsource = #{appsource,jdbcType=VARCHAR},",
            "appstatus = #{appstatus,jdbcType=SMALLINT}",
            "WHERE appid = #{appid,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Ems4jApplication record);
}